/*
 * Created on 2006-11-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ims.iaa.Picture;

import java.io.FileInputStream;
import java.io.IOException;
/**
 * @author changsheng gong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PicUtil {

	public static PicModel getFileAttributes(byte[] content)
	{
	    PicModel model = new PicModel();
		String picextendname = null;
		
		picextendname = getFileExtendName(content);
		
		int picwidth,picheight;
		picwidth = 0;
		picheight = 0;
		
		if (picextendname.equals("GIF"))
		{
			//picwidth position
			picwidth = getFileAttribute(content,7,2,picextendname); 
			//picheight position
			picheight = getFileAttribute(content,9,2,picextendname);
		}
		if (picextendname.equals("JPG"))
		{
			picwidth = getFileAttribute(content,166,2,picextendname);
			picheight = getFileAttribute(content,164,2,picextendname);
		}
		if (picextendname.equals("BMP"))
		{
			picwidth = getFileAttribute(content,19,2,picextendname);
			picheight = getFileAttribute(content,23,2,picextendname);
		}
		if (picextendname.equals("PNG"))
		{
			picwidth = getFileAttribute(content,19,2,picextendname);
			picheight = getFileAttribute(content,23,2,picextendname);
		}
		
		model.setpicsExtendName(picextendname);
		model.setpicsWidth(picwidth);
		model.setpicsHeight(picheight);
		return model;
	}

	private static byte[] readFromFile(String fileName) throws IOException
	{
		FileInputStream fin = new FileInputStream(fileName);
		byte [] buf = new byte[fin.available()];
		fin.read(buf);
		fin.close();
		return buf;
	}

	private static String getFileExtendName(byte[] byte1)
	{
		String strFileExtendName;
		strFileExtendName = null;
		
		//header bytes contains GIF87a or GIF89a?
		if ((byte1[0] == 71)&&(byte1[1] == 73)&&(byte1[2] == 70)&&(byte1[3] == 56)&&((byte1[4] == 55)||(byte1[4] == 57))&&(byte1[5] == 97))
		{
		    strFileExtendName = "GIF";
		}
		//header bytes contains JFIF?
		if ((byte1[6] == 74)&&(byte1[7] == 70)&&(byte1[8] == 73)&&(byte1[9] == 70))
		{
		    strFileExtendName = "JPG";
		}
		//header bytes contains BM?
		if ((byte1[0] == 66)&&(byte1[1] == 77))
		{
		    strFileExtendName = "BMP";
		}
		//header bytes contains PNG?
		if ((byte1[1] == 80)&&(byte1[2] == 78)&&(byte1[3] == 71))
		{
		    strFileExtendName = "PNG";
		}
		
		return strFileExtendName;
	}

	private static int getFileAttribute(byte[] byte2,int n,int m,String fileextendname)
	{
		int j,FileAttributeValue;
		j = 0;
		FileAttributeValue = 0;
		String str,str1;
		str = "";
		str1 = "";
		
		for (int k = 0; k < m; k ++)
		{
			if (byte2[n-k] < 0)
			{
				j = byte2[n-k];
				j = j + 256;
			}
			else
			{
			    j = byte2[n-k];
			}
			str1 = Integer.toHexString(j);
			if (str1.length() < 2)
			{
			    str1 = "0" + str1;
			}
			
			if(fileextendname.equalsIgnoreCase("JPG")||fileextendname.equalsIgnoreCase("PNG"))
			{
			    str = str1 + str;
			}
			else
			{
			    str = str + str1;
			}
		}
		FileAttributeValue = HexToDec(str);
		return FileAttributeValue;
	}

	private static int HexToDec(String cadhex)
	{
		int n, i, j,k,decimal;
		String CADHEX1;
		n = 0;
		i = 0;
		j = 0;
		k = 0;
		decimal = 0;
		CADHEX1 = null;
		n =cadhex.length();
		CADHEX1 = cadhex.trim().toUpperCase();
		
		while(i < n)
		{
			j = CADHEX1.charAt(i);
			if ((j >= 48) && (j < 65))
			{
			    j = j - 48;
			}
			if (j >= 65)
			{
			    j = j - 55;
			}
			i = i + 1;
			
			k = 1;
			for (int m = 0; m < (n-i); m ++)
			{
			    k = 16 * k;
			}
			decimal = j*k + decimal;
		}
		
		return decimal;
	}

	private static String getPicColor(int color)
	{
		int k;
		k = 1;
		String piccolor;
		piccolor = null;
		
		for(int m=0;m<color;m++)
		{
		    k=2*k;
		}
		Integer kk;
		kk = null;
		if (k >= 1048576)
		{
			k = k / 1048576;
			kk = new Integer(k);
			piccolor = kk.toString() + "M";
		}
		else if (k >= 1024)
		{
			k = k / 1024;
			kk = new Integer(k);
			piccolor = kk.toString() + "K";
		}
		else if (k > 0)
		{
			kk = new Integer(k);
			piccolor = kk.toString();
		}
		
		return piccolor;
	}
}
